####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A11
###
####################################################################################


library(dplyr)
library(ggplot2)
library(miceadds)
library(rlist)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################
# Bounds
################################################################################

# Extract the averge number of hlm residents per hlm dwelling
df_did$temp = mean(df_did$num_pp_hlm/df_did$num_hlm, na.rm = T)

# Merge it back to the main dataset and calculate the imputed value for those missing
temp = aggregate(temp ~ CODGEO, data=df_did, mean)
df_did = merge(df_did, temp, on='CODGEO')

df_did$num_pp_hlm_imp = ifelse(is.na(df_did$num_pp_hlm)==T,
  df_did$temp * df_did$num_hlm,
  df_did$num_pp_hlm)

df_did$temp = NULL
rm(temp)


# Share of new people in hlm, arriving after the policies, i.e. excess in hlm population
temp = df_did[df_did$policy_period_ind==-1, c('CODGEO', 'num_pp_hlm_imp')]
colnames(temp) = c('CODGEO', 'temp')
df_did = merge(df_did, temp, on='CODGEO', all.x = T)

hlm_new_pp_share = ifelse(df_did$policy_period_ind>=0,
  (df_did$num_pp_hlm_imp - df_did$temp)/df_did$pop, 0)
hlm_new_pp_share[df_did$sruT==0] = 0
hlm_new_pp_share[hlm_new_pp_share<0] = 0


# Assume that Share of people in hlm == Share of voters in hlm 
# and recover the number of votes expressed by poeple in hlm
hlm_votes = df_did$expr_votes*hlm_new_pp_share

# Recover the number of votes for fn
fn_votes = df_did$fn*df_did$expr_votes

# Calculate the vote share wihtout non hlm people
# 1 -  Assuming that the share of people in HLM voting for FN == maximum FN vote share
max.vs = max(df_did$fn, na.rm = T)
df_did$fn_upper =  (fn_votes-max.vs*hlm_votes)/ (df_did$expr_votes - hlm_votes)
df_did$fn_upper[df_did$fn_upper<0] = 0

# 2 -  Assuming that the share of people in HLM voting for FN == minimum FN vote share
min.vs = min(df_did$fn, na.rm = T)
df_did$fn_lower =  (fn_votes-min.vs*hlm_votes)/ (df_did$expr_votes - hlm_votes)
df_did$fn_lower[df_did$fn_lower<0] = 0


################################################################################
# Plot Functions
################################################################################

makeplot_heter = function(model_output, hete_var){
  groups = c(paste0('Low ', hete_var), paste0('Medium ', hete_var), paste0('High ', hete_var))
  container = lapply(model_output, function(x) rbind(cbind(summary(x)[2:6,], period = c(-4, -3, -2, 1, 3)), c(0,0,NA,NA,-1)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(groups, each=6))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = groups)
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f, shape = group.f))+
    geom_point(position = position_dodge(width = 0.5)) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), width=.1, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application") + 
    ylab("") +
    theme_light()+
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
  }

  
################################################################################
# All by immigration levels
################################################################################


# 1 - Upper bound
modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$imm_quant_99==n),]
  temp = lm.cluster(data=df1, fn_upper ~  m4 + m3 + m2 + p1 + p2 + p3 + running + 
              factor(CODGEO) + factor(policy_period), cluster = 'CODGEO')
  modelli[[n]] = temp
}

makeplot_heter(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA11a.pdf'), height=7, width=10)



# 1 - Lower bound
modelli = list()
for (n in c(1,2,3)){
  print(paste0('Immigation tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$imm_quant_99==n),]
  temp = lm.cluster(data=df1, fn_lower ~  m4 + m3 + m2 + p1 + p2 + p3 + running + 
                      factor(CODGEO) + factor(policy_period), cluster = 'CODGEO')
  modelli[[n]] = temp
}

makeplot_heter(modelli, 'Immigration')
ggsave(paste0(wd_res, '/figures/figA11b.pdf'), height=7, width=10)

